package ar.com.solidussource.howmuchmobile.services;

import ar.com.solidussource.howmuchmobile.common.entities.Location;
import ar.com.solidussource.howmuchmobile.common.entities.Province;
import ar.com.solidussource.howmuchmobile.common.exception.ServiceException;
import ar.com.solidussource.howmuchmobile.services.helper.RestServiceHelper;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 07/11/12
 * Time: 12:08
 * To change this template use File | Settings | File Templates.
 */
public class LocationRestService implements RestService {
    private static String SERVICE_ENDPOINT = SERVER_ENDPOINT+"/location";
    private static String OP_LIST_ENDPOINT = SERVICE_ENDPOINT+"/list.json";

    public List<Location> getLocations(Integer provinceId, Integer pageNumber, Integer size) throws ServiceException {
        List<Location> locations = new ArrayList<Location>();

        HttpClient httpClient = new DefaultHttpClient();

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("provinceId", provinceId);
        params.put("pageNumber", pageNumber);
        params.put("size", size);

        String url = RestServiceHelper.addParamToUrl(OP_LIST_ENDPOINT, params);

        HttpGet get = new HttpGet(url);

        try{
            HttpResponse resp = httpClient.execute(get);
            String respStr = EntityUtils.toString(resp.getEntity());

            JSONArray respJSON = new JSONArray(respStr);

            for(int i=0; i<respJSON.length(); i++) {
                JSONObject locationJSON = respJSON.getJSONObject(i);

                Location location = new Location();
                location.setId(Integer.valueOf(locationJSON.getInt("id")));
                location.setName(locationJSON.getString("name"));

                Province province = new Province();
                JSONObject provinceJSON = locationJSON.getJSONObject("province");

                province.setId(provinceJSON.getInt("id"));
                province.setName(provinceJSON.getString("name"));

                location.setProvince(province);

                locations.add(location);
            }

        } catch (Exception e) {
            throw new ServiceException(e);
        }

        return locations;
    }

    public Location[] getLocationsAsArray(Integer provinceId, Integer pageNumber, Integer size) throws ServiceException {
        List<Location> locations = getLocations(provinceId, pageNumber, size);

        Location[] locationsAsArray = new Location[locations.size()];

        int i = 0;

        Iterator<Location> ite = locations.iterator();

        while (ite.hasNext()) {
            Location location = ite.next();
            locationsAsArray[i] = location;
            i++;
        }

        return locationsAsArray;
    }

}
